/**
*
* @author zzhijie
* @date 2021-07-01
*/

<template>
  <div
    class="size-item"
    :class="'border-'+params.borderImage"
    :style="getItemStyle">
    <div class="bg" :style="getBgStyle"></div>
    <div class="border" :style="getBorderStyle"></div>
    <slot></slot>
  </div>
</template>

<script>

export default {
  name: "sizeItem",
  props: {
    params: {
      type: Object,
    },
    groupSize: {
      type: Object
    },
    ignoreSize: {
      type: Boolean
    },
    ratio: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      relationSize: {...this.groupSize},
      // ratio: 1,
      zoomFlag: false,
      baseSize: {...this.params.size}
    }
  },
  computed: {
    borderImgList() {
      return this.$store.state.borderImgList || [];
    },
    getItemStyle() {
      if (this.ignoreSize) return null;
      let {top, left, width, height, zIndex} = this.params.size || {};
      let {top: gTop = 0, left: gLeft = 0} = this.relationSize || {};
      return {
        transform: `scale(${this.ratio})`,
        top: top - gTop + 'px',
        left: left - gLeft + 'px',
        width: width + 'px',
        height: height + 'px',
      };
    },
    getBgStyle() {
      let {backgroundColor, backgroundImage, opacity, size = {}, borderImage} = this.params || {};
      let {width, height} = size;
      let {path = ""} = this.borderImgList.find(v => v.id === borderImage) || {};
      path = path.replace(/\$\{(width|height)\s?-\s?([0-9.]+)\}/g, function (match, $1, $2) {
        if ($1 === "width") {
          return width - (+$2);
        } else if ($1 === "height") {
          return height - (+$2);
        }
      });
      return {
        backgroundColor: backgroundColor,
        backgroundImage: backgroundImage ? `url(${backgroundImage})` : null,
        opacity: opacity,
        clipPath: `polygon(${path})`
      }
    },
    getBorderStyle() {
      let {size = {}, borderImage} = this.params || {};
      let {imgUrl = "", borderWidth = "", path = ""} = this.borderImgList.find(v => v.id === borderImage) || {};
      let borderWidthList = borderWidth.split(",");
      return {
        borderWidth: borderWidthList.map(v => v + "px").join(" "),
        borderImage: `url(${imgUrl}) ${borderWidthList.join(" ")} repeat`,
      }
    }
  },
  methods: {},
  mounted() {

  },
}
</script>

<style lang="scss" scoped>
.size-item {
  position: absolute;
  transition: all .3s;
  /*overflow: hidden;*/
  transform-origin: 0 0;
  width: 100%;
  height: 100%;

  .border {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    border-style: solid;
    border-color: transparent;
    //border-top: 50px solid transparent;
    //border-right: 260px solid transparent;
    //border-bottom: 50px solid transparent;
    //border-left: 30px solid transparent;
    //border-image: url("../../img/border/1.png") 50 260 50 30 repeat;
    box-sizing: border-box;
  }

  &.zoom-out {
    cursor: zoom-out;
    z-index: 999 !important;
  }

  &.zoom-in {
    cursor: zoom-in;
  }

  .bg {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: -1;
    //background-size: cover;
    background-size: 100% 100%;
  }
}
</style>
